package com.ruoyi.kjy.service.impl;

//import cn.easyes.core.biz.PageInfo;
//import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.kjy.domain.Demand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.kjy.mapper.AchievementEsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.kjy.mapper.AchievementMapper;
import com.ruoyi.kjy.domain.Achievement;
import com.ruoyi.kjy.service.IAchievementService;

import java.awt.*;
import java.io.Serializable;
import java.util.*;
import java.util.List;

/**
 * 科技成果管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-06
 */
@Service
public class AchievementServiceImpl extends ServiceImpl<AchievementMapper, Achievement> implements IAchievementService {
//    @Autowired
//    AchievementEsMapper achievementEsMapper;
    @Autowired
    private AchievementMapper achievementMapper;

    @Override
    public boolean updateById(Achievement entity) {
        //return super.updateById(entity) && achievementEsMapper.updateById(entity) > 0;
        return super.updateById(entity);
    }

//    @Override
//    public boolean removeById(Serializable id) {
//        achievementEsMapper.deleteBatchIds(Arrays.asList(id));
//        return super.removeById(id) ;
//    }

    @Override
    public boolean removeByIds(Long[] ids) {
        List<Serializable> idList = Arrays.asList(ids);
        //achievementEsMapper.deleteBatchIds(idList);
        return super.removeByIds(idList);
    }

    @Override
    public boolean save(Achievement entity) {
        boolean save = super.save(entity);
        //Integer insert = achievementEsMapper.insert(entity);
        return save;
    }
    @Override
    public IPage<Achievement> page(int current, int size, Achievement achievement) {
        Page<Achievement> page = new Page<>(current, size);

        QueryWrapper<Achievement> queryWrapper = new QueryWrapper<>();
        //queryWrapper.setEntity(achievement);
        String searchWord = achievement.getSearchWord();
        if (searchWord != null && !"".equals(searchWord)) {
            queryWrapper.like("title",searchWord);
        }
        List<String> location = achievement.getLocation();
        if (location != null && location.size() != 0) {
            queryWrapper.in("location", location.get(0));
        }
        Integer state = achievement.getState();
        if (state != null) {
            queryWrapper.eq("state", state);
        }
        List<String> industrySector = achievement.getIndustrySector();
        if (industrySector != null && industrySector.size() != 0) {
            //queryWrapper.in("industry_sector", industrySector.get(0));
            queryWrapper.apply("industry_sector REGEXP {0}", industrySector.get(0));
        }
        Long author = achievement.getAuthor();
        if (author != null) {
            queryWrapper.eq("author", author);
            // 只有用户查看自己的成果时，才按照时间降序排序，否则，按照es搜索出来的顺序返回
            //queryWrapper.orderByDesc("create_time");
        }
        queryWrapper.orderByDesc("create_time");
        System.out.println("sql:"+queryWrapper.getSqlSelect());
        IPage<Achievement> resultPage = achievementMapper.selectPage(page, queryWrapper);

        return resultPage;
    }
//    @Override
//    public PageInfo<Achievement> espage(Integer current, Integer size, Achievement achievement) {
//        LambdaEsQueryWrapper<Achievement> lambdaEsQueryWrapper = new LambdaEsQueryWrapper<>();
//        String searchWord = achievement.getSearchWord();
//        if (searchWord != null && !"".equals(searchWord)) {
//            lambdaEsQueryWrapper.queryStringQuery(searchWord);
//        }
//        List<String> location = achievement.getLocation();
//        if (location != null && location.size() != 0) {
//            lambdaEsQueryWrapper.matchPhrase("location", location.get(0));
//        }
//        Integer state = achievement.getState();
//        if (state != null) {
//            lambdaEsQueryWrapper.matchPhrase("state", state);
//        }
//        List<String> industrySector = achievement.getIndustrySector();
//        if (industrySector != null && industrySector.size() != 0) {
//            lambdaEsQueryWrapper.matchPhrase("industrySector", industrySector.get(0));
//        }
//        Long author = achievement.getAuthor();
//        if (author != null) {
//            lambdaEsQueryWrapper.matchPhrase("author", author);
//            // 只有用户查看自己的成果时，才按照时间降序排序，否则，按照es搜索出来的顺序返回
//            lambdaEsQueryWrapper.orderByDesc(Achievement::getCreateTime);
//        }
//        PageInfo<Achievement> achievementPageInfo=new PageInfo<Achievement>();
//        try{
//            achievementPageInfo = achievementEsMapper.pageQuery(lambdaEsQueryWrapper, current, size);
//        }finally {
//            return achievementPageInfo;
//        }
//    }

    @Override
    public void updateNickName(Achievement achievement) {
//        LambdaEsQueryWrapper<Achievement> lambdaEsQueryWrapper = new LambdaEsQueryWrapper<>();
//        lambdaEsQueryWrapper.matchPhrase("author", achievement.getAuthor());
//        List<Achievement> achievements = achievementEsMapper.selectList(lambdaEsQueryWrapper);
//        for (Achievement achievement1 : achievements) {
//            achievement1.setAuthorName(achievement.getAuthorName());
//        }
//        achievementEsMapper.updateBatchByIds(achievements);
        QueryWrapper<Achievement> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("author",achievement.getAuthor());
        List<Achievement> achievements= this.list(queryWrapper);
        for (Achievement item : achievements) {
            item.setAuthorName(achievement.getAuthorName());
        }
        this.updateBatchById(achievements);
    }
}
